﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace CoolCode
{
    public  class DateFormat
    {
        public static string GetSysDateFormat()
        {
            var dateFormat = CultureInfo.CurrentCulture.DateTimeFormat;
            string dateSep = dateFormat.DateSeparator;
            string dateMask = dateFormat.ShortDatePattern;
            return dateMask;
        }

        public static string GetSysYearMonthFormat()
        {
            string df = GetSysDateFormat();
            string sep = CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator;
            string newDf = df.Replace(sep + "dd", "").Replace("dd" + sep, "");//remove day
            return newDf;
        }

        public static string ToSystemDateFormat(string date)
        {
            var dateFormat = CultureInfo.CurrentCulture.DateTimeFormat;
            string dateMask = dateFormat.ShortDatePattern;
            DateTime dateTime;
            if (DateTime.TryParse(date, out dateTime))
                return dateTime.ToString(dateMask, CultureInfo.InvariantCulture);
            return date;
        }

        public static string ToSystemDateTimeFormat(string date)
        {
            string dateMask = GetSysDateFormat() + " HH:mm:ss";
            DateTime dateTime;
            if (DateTime.TryParse(date, out dateTime))
                return dateTime.ToString(dateMask, CultureInfo.InvariantCulture);
            return date;
        }

        public static string ToSqlDateTime(DateTime dt)
        {
            string str = dt.ToString("yyyy-MM-dd HH:mm:ss.ffff");
            return str.Remove(str.Length - 1);
        }

    }
}
